package com.hupu.jrs.provider

import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.os.ParcelFileDescriptor
import java.io.File
import java.io.FileNotFoundException

/**
 * Created by sll on 2016/6/16.
 */
class LocalImageProvider : ContentProvider() {

    @Throws(FileNotFoundException::class)
    override fun openFile(uri: Uri, mode: String): ParcelFileDescriptor? {
        val file = File(uri.path)
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
    }

    override fun onCreate(): Boolean {
        return true
    }

    override fun delete(uri: Uri, s: String?, `as`: Array<String>?): Int {
        throw UnsupportedOperationException("Not supported by this provider")
    }

    override fun getType(uri: Uri): String? {
        throw UnsupportedOperationException("Not supported by this provider")
    }

    override fun insert(uri: Uri, contentvalues: ContentValues?): Uri? {
        throw UnsupportedOperationException("Not supported by this provider")
    }

    override fun query(uri: Uri, `as`: Array<String>?, s: String?, as1: Array<String>?, s1: String?): Cursor? {
        throw UnsupportedOperationException("Not supported by this provider")
    }

    override fun update(uri: Uri, contentvalues: ContentValues?, s: String?, `as`: Array<String>?): Int {
        throw UnsupportedOperationException("Not supported by this provider")
    }

    companion object {
        private val URI_PREFIX = "content://com.gzsll.hupu"

        fun constructUri(url: String): String {
            val uri = Uri.parse(url)
            return if (uri.isAbsolute) url else URI_PREFIX + url
        }
    }
}
